package com.zib.vread.downloadmanagedemo.Update;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.widget.Toast;

/**
 * Created by vread on 2016/8/24.
 * 1.判断是否需要更新
 * 1.1 获取当前版本号
 * 1.2 获取网络版本号
 */
public abstract class UpdateManager {
    private boolean netWorkIsWifi;
    private Context context = null;
    private MyserviceConnection serviceConnection = new MyserviceConnection();
    private UpdateService mService = null;

    public UpdateManager(Context context) {
        this.context = context;
    }

    public int getLocalVerCode(){
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        int version = packInfo.versionCode;
        return version;
    }

    public abstract int getServiceVerCode();

    public void isUpdateVersion(){
        if (getServiceVerCode() > getLocalVerCode()) {
            doUpdateVersion();
        } else {
            notNewVersionShow();
        }
    }

    private void doUpdateVersion() {
        netWorkIsWifi = NetWorkUtil.getNetworkType(context) == NetWorkUtil.NetType.NETWORK_WIFI;
        Dialog dialog = new AlertDialog.Builder(context)
                .setTitle("软件更新")
                .setMessage("发现新的版本，是否更新？")
                .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (netWorkIsWifi) {
                            Intent intent = new Intent();
                            intent.setClass(context, UpdateService.class);
                            context.bindService(intent,
                                    serviceConnection, Context.BIND_AUTO_CREATE);
                        } else {
                            Toast.makeText(context, "当前为非WiFi状态，请切换为WiFi", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .create();
        dialog.show();
    }

    private void notNewVersionShow() {
        Toast.makeText(context, "已是最新版本", Toast.LENGTH_SHORT).show();
    }


    class MyserviceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            UpdateService.LocalBinder binder = (UpdateService.LocalBinder) service;
            mService = binder.getService();
            mService.showNotification();
            mService.downloadApk();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    }


//    public void setIsWifi(boolean b) {
//        if (!b) {
//            Toast.makeText(context, "当前为非WiFi状态，请切换为WiFi", Toast.LENGTH_SHORT).show();
//        }
//        netWorkIsWifi = b;
//    }

    public void unbindService() {
        context.unbindService(serviceConnection);
    }

}
